/**
 * 
 */
package com.googlecode.xcarve.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xinyun.du
 *
 */
public class DoveThreadFactory implements ThreadFactory{
	
	private static final AtomicInteger POOLS_SEQ = new AtomicInteger(1);
	
	private static final AtomicInteger THREAD_SEQ = new AtomicInteger(1);
	
	private String threadName;
	
	private ThreadGroup threadGroup;
	
	public DoveThreadFactory(String prefix) {
		threadName = prefix +"- pools: "+ POOLS_SEQ.incrementAndGet();
		SecurityManager s = System.getSecurityManager();
		threadGroup = ( s == null ) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup(); 
	}
	
	@Override
	public Thread newThread(Runnable runnable) {
		Thread t = new Thread(threadGroup, runnable , threadName +" — thread: "+ THREAD_SEQ.incrementAndGet());
//		t.setDaemon(true);
		return t;
	}
	

}
